﻿DECLARE @HIS_ZYNum              INT
DECLARE @HIS_PatientName        VARCHAR(100)
DECLARE @HIS_AdmissionDate      DATETIME
DECLARE @HIS_AdmissionDeptName  VARCHAR(100)
DECLARE @HIS_StayDeptName       VARCHAR(100)
DECLARE @HIS_AttendingPhysician VARCHAR(100)
DECLARE @HIS_LengthOfStay       INT
DECLARE @HIS_BedNo              VARCHAR(100)
DECLARE @Var_JZDate             VARCHAR(100)
DECLARE @Var_PatientId          VARCHAR(100)
DECLARE @HIS_CostMoney          VARCHAR(100)
DECLARE @HIS_PreMoney           VARCHAR(100)

SET @Var_JZDate = @JZDate_In
SET @Var_PatientId = @PatientId_In

select top 1 @HIS_ZYNum=m01
      ,@HIS_PatientName=RTRIM(m04)
      ,@HIS_AdmissionDate=m11
      ,@HIS_AdmissionDeptName=RTRIM(m13)
      ,@HIS_StayDeptName=RTRIM(m17)
      ,@HIS_AttendingPhysician=RTRIM(m35)
      ,@HIS_LengthOfStay=m22
	  ,@HIS_BedNo=M18
	  ,@HIS_CostMoney=M25
      ,@HIS_PreMoney=M27
  from MBASE(nolock) where PatientID = @Var_PatientId order by M01 desc

select @HIS_ZYNum AS ZYNum
      ,@HIS_PatientName AS PatientName
      ,@HIS_AdmissionDate AS AdmissionDate
      ,@HIS_AdmissionDeptName AS AdmissionDeptName
      ,@HIS_StayDeptName AS StayDeptName
      ,@HIS_AttendingPhysician AS AttendingPhysician
      ,@HIS_LengthOfStay AS LengthOfStay
	  ,@HIS_BedNo AS BedNo
	  ,@HIS_CostMoney AS CostMoney
      ,@HIS_PreMoney AS PreMoney
      ,RTRIM(a.lykscode) as DeptId
	    ,RTRIM(a.lyksname) as DeptName
	    ,RTRIM(a.yscode) as DoctId
	    ,RTRIM(a.ysname) as DoctName
	    ,a.jzdate as JZDate
	    ,RTRIM(a.goodsno) as ItemId
	    ,RTRIM(a.goodsname) as ItemName
	    ,RTRIM(a.kmname) as ItemType
	    ,RTRIM(a.unitname) as Unit
	    ,a.ypprice as Price
	    ,a.YPCount as Num
      ,a.YPMoney
  from ZYCFYPK a (nolock)
 where ZYNum=@HIS_ZYNum and
	   a.DelDate is null and
     cast(a.jzdate as date) like @Var_JZDate /*and
     Datediff(day,a.jzdate,getdate())<15*/
union all
select @HIS_ZYNum AS ZYNum
      ,@HIS_PatientName AS PatientName
      ,@HIS_AdmissionDate AS AdmissionDate
      ,@HIS_AdmissionDeptName AS AdmissionDeptName
      ,@HIS_StayDeptName AS StayDeptName
      ,@HIS_AttendingPhysician AS AttendingPhysician
      ,@HIS_LengthOfStay AS LengthOfStay
	  ,@HIS_BedNo AS BedNo
	  ,@HIS_CostMoney AS CostMoney
      ,@HIS_PreMoney AS PreMoney
      ,RTRIM(a.fskscode) as DeptId
	    ,RTRIM(a.fsksname) as DeptName
	    ,RTRIM(a.yscode) as DoctId
	    ,RTRIM(a.ysname) as DoctName
	    ,a.jzdate as JZDate
	    ,RTRIM(a.checkno) as ItemId
	    ,RTRIM(a.checkname) as ItemName
	    ,RTRIM(a.kmname) as ItemType
	    ,RTRIM(a.unitname) as Unit
	    ,a.checkprice as Price
	    ,a.checkcount as Num
      ,a.CheckMoney
  from ZYCHECK a (nolock)
 where ZYNum=@HIS_ZYNum and
	   a.DelDate is null and
     cast(a.jzdate as date) like @Var_JZDate /*and
     Datediff(day,a.jzdate,getdate())<15*/  
